package cn.tedu.product.base;

import cn.tedu.product.pojo.dto.OrderDTO;
import cn.tedu.product.pojo.dto.AdminSelectDTO;
import cn.tedu.product.pojo.dto.UserSelectDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Component
public class Utils {
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;
    public static Object ObjisNull(Object object){
        if (object==null)
            return true;

        Class cls=object.getClass();
        Field[] fields=cls.getDeclaredFields();
        for (Field field:fields){
            field.setAccessible(true);
            try {
                Object o=field.get(object);
                if (o!=null){
                    return field.getName();
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }


    public static boolean ObjAllnotisNull(Object object){
        if (object==null)
            return false;

        Class cls=object.getClass();
        Field[] fields=cls.getDeclaredFields();
        for (Field field:fields){
            field.setAccessible(true);
            try {
                Object o=field.get(object);
                if (o==null){
                    return false;
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            field.setAccessible(false);
        }
        return true;
    }


    public static List<String> WhosnotNUll(OrderDTO orderDTO){
        List<String> list=new ArrayList<>();
        if (orderDTO==null)
            return list;

        Class cls=orderDTO.getClass();
        Field[] fields=cls.getDeclaredFields();
        for (Field field:fields){
            field.setAccessible(true);
            try {
                Object o=field.get(orderDTO);
                if (o!=null){
                    list.add(field.getName());
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return list;
    }


    public static boolean isConStr(List<String> list,String str){
        for (String s:list){
            if (s.equals(str)){
                return true;
            }
        }
        return false;
    }



    public static String[] getSTandET(String start){
        String startTime=start+" 00:00:00";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String endTime;
        try {
            Date startDate=sdf.parse(startTime);
            Calendar calendar=Calendar.getInstance();
            calendar.setTime(startDate);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            Date endDate=calendar.getTime();
            endTime=sdf.format(endDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return new String[]{startTime,endTime};
    }


    public static AdminSelectDTO getorderSelectDTO(Object object){
        AdminSelectDTO orderSelectDTO=new AdminSelectDTO();

        if (object instanceof UserSelectDTO){
            if (((UserSelectDTO)object).getCreatedAt()!=null){
                String[] dateStr=Utils.getSTandET(((UserSelectDTO)object).getCreatedAt());
                BeanUtils.copyProperties((UserSelectDTO)object,orderSelectDTO);
                orderSelectDTO.setStartTime(dateStr[0]);
                orderSelectDTO.setEndTime(dateStr[1]);
            }
        }else {
            if (((OrderDTO)object).getCreatedAt()!=null){
                String[] dateStr=Utils.getSTandET(((OrderDTO)object).getCreatedAt());
                BeanUtils.copyProperties((OrderDTO)object,orderSelectDTO);
                orderSelectDTO.setStartTime(dateStr[0]);
                orderSelectDTO.setEndTime(dateStr[1]);
            }
        }

        return orderSelectDTO;
    }


    @Bean
    public ValueOperations<String, Serializable> getops(){
        ValueOperations<String, Serializable> ops=redisTemplate.opsForValue();
        return ops;
    }
}
